import 'package:fate_blind_box/chat_pages/person_home/view.dart';
import 'package:fate_blind_box/config/event_bus.dart';
import 'package:fate_blind_box/config/event_keys.dart';
import 'package:fate_blind_box/network/http_config.dart';
import 'package:fate_blind_box/ui/login/login_by_pwd.dart';
import 'package:fate_blind_box/utils/privte_dialog.dart';
import 'package:flutter/material.dart';
import 'package:im_flutter_sdk/im_flutter_sdk.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:get/get.dart';
import 'logic.dart';


class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

final logic = Get.find<HomeLogic>();
final state = Get.find<HomeLogic>().state;

class _HomePageState extends State<HomePage> {
  @override
  void initState() {
    // TODO: implement initState
    print('登录完毕初始化');
    super.initState();
    logic.getToken();
    initData();
    logic.addChatListener();
  }
  initData() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var privacyPolicyDialog = prefs.getBool('PrivacyPolicyDialog');
    // 同意过隐私协议
    if (privacyPolicyDialog==null||privacyPolicyDialog == false) {
      showDialog();
    }
  }

  showDialog() {
    dialogShow(context);
  }


  @override
  Widget build(BuildContext context) {
    if(HttpConfig.token.isEmpty){
      return const LoginByPwd();
    }else{
      return const PersonHomePage();
    }
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    EMClient.getInstance.chatManager.removeMessageEvent(state.tag);
    EventBus.instance.removeListener(eventName: EventKeys.chatUpdateBadge);
  }
}
